package com.atguigu.gulimallcart.controller;

import com.atguigu.gulimallcart.feign.ProductSkuFeignService;
import com.atguigu.gulimallcart.service.CartService;
import com.atguigu.gulimallcart.vo.Cart;
import com.atguigu.gulimallcart.vo.CartItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

@Slf4j
@Controller
public class indexController {


    @Autowired
    CartService cartService;

    @Autowired
    ProductSkuFeignService productSkuFeignService;


    @ResponseBody
    @GetMapping("/getCartItem")
    public List<CartItem>getCartItems()throws Exception{
        Cart carts = cartService.getCarts();
        //购物项
        List<CartItem> items = carts.getItems();
        List<CartItem> collect = items.stream().filter(item -> {
            //过滤出来被选中的商品
            return item.getCheck();
        }).map(item->{
            //ToDo:商品的价格必须获取最新的价格
            //远程查询商品的最新价格
            item.setPrice(productSkuFeignService.getProductPrice(item.getSkuId()));
            return item;
        }).collect(Collectors.toList());
        return collect;
    }

    //跳到购物车列表的页面
    @GetMapping("/cartList.html")
    public String toCartListPage(Model model) throws ExecutionException, InterruptedException {
        Cart cart=cartService.getCarts();
        model.addAttribute("cart",cart);
        log.info("购物车对象:"+cart);
        return "cartList";
    }

    /**
     * @return
     */
    @GetMapping("/success.html")
    public String toSuccessPage() {
        return "success";
    }

    /**
     * * 添加到购物车
     * *RedirectAttributes
     * addAttribute():在链接后面拼接上一个skuId
     * addFlashAttribute():添加一个属性，只能使用一次。模拟session
     * * @param skuId 要添加的商品
     * * @param num   添加的数量
     * * @param model
     */
    @GetMapping("/addCartItem")
    public String addCartItem(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num, RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {
        cartService.addCartItem(skuId, num);
        //使用重定向，将sku编号拼接在链接地址的后面
        redirectAttributes.addAttribute("skuId", skuId);
        return "redirect:http://cart.gulimall.com/toSuccess";
    }


    @GetMapping("/toSuccess")
    public String toSuccessPage(@RequestParam("skuId") Long skuId, Model model) {
        //获取cartItem对象
        CartItem cartItem = cartService.getItemBySkuId(skuId);
        model.addAttribute("item", cartItem);
        return "success";
    }

    @GetMapping("/changeCartItem")
    public String changeCheckedStatus(@RequestParam("skuId") Long skuId,
                                      @RequestParam("checked") Integer checked){
        cartService.changeStatus(skuId,checked);
        return "redirect:http://cart.gulimall.com/cartList.html";
    }

    @GetMapping("/changeCartItemNum")
    public String changeNumStatus(@RequestParam("skuId") Long skuId,
                                      @RequestParam("num") Integer num){
        cartService.changeNum(skuId,num);
        return "redirect:http://cart.gulimall.com/cartList.html";
    }

    @GetMapping("/deleteCartItem")
    public String deleteCartItem(@RequestParam("skuId") Long skuId){
        cartService.delCartItem(skuId);
        return "redirect:http://cart.gulimall.com/cartList.html";
    }



}
